<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="触摸屏数量" prop="pTouchscreennum">
      <el-input v-model="dataForm.pTouchscreennum" placeholder="触摸屏数量"></el-input>
    </el-form-item>
    <el-form-item label="专区数量" prop="pSpecialzonenum">
      <el-input v-model="dataForm.pSpecialzonenum" placeholder="专区数量"></el-input>
    </el-form-item>
    <el-form-item label="手机app数量" prop="pTelappnum">
      <el-input v-model="dataForm.pTelappnum" placeholder="手机app数量"></el-input>
    </el-form-item>
    <el-form-item label="微刊数量" prop="pMicrojournalnum">
      <el-input v-model="dataForm.pMicrojournalnum" placeholder="微刊数量"></el-input>
    </el-form-item>
    <el-form-item label="平板app数量" prop="pTabappnum">
      <el-input v-model="dataForm.pTabappnum" placeholder="平板app数量"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          pId: 0,
          pTouchscreennum: '',
          pSpecialzonenum: '',
          pTelappnum: '',
          pMicrojournalnum: '',
          pTabappnum: ''
        },
        dataRule: {
          pTouchscreennum: [
            { required: true, message: '触摸屏数量不能为空', trigger: 'blur' }
          ],
          pSpecialzonenum: [
            { required: true, message: '专区数量不能为空', trigger: 'blur' }
          ],
          pTelappnum: [
            { required: true, message: '手机app数量不能为空', trigger: 'blur' }
          ],
          pMicrojournalnum: [
            { required: true, message: '微刊数量不能为空', trigger: 'blur' }
          ],
          pTabappnum: [
            { required: true, message: '平板app数量不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.pId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.pId) {
            this.$http({
              url: this.$http.adornUrl(`/com.emp/productsaledata/info/${this.dataForm.pId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.pTouchscreennum = data.productsaledata.pTouchscreennum
                this.dataForm.pSpecialzonenum = data.productsaledata.pSpecialzonenum
                this.dataForm.pTelappnum = data.productsaledata.pTelappnum
                this.dataForm.pMicrojournalnum = data.productsaledata.pMicrojournalnum
                this.dataForm.pTabappnum = data.productsaledata.pTabappnum
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/com.emp/productsaledata/${!this.dataForm.pId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'pId': this.dataForm.pId || undefined,
                'pTouchscreennum': this.dataForm.pTouchscreennum,
                'pSpecialzonenum': this.dataForm.pSpecialzonenum,
                'pTelappnum': this.dataForm.pTelappnum,
                'pMicrojournalnum': this.dataForm.pMicrojournalnum,
                'pTabappnum': this.dataForm.pTabappnum
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
